<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Api\V1\Wechat\WechatController;
use App\Models\UserProfile;
use App\Models\Box;
use App\Models\BoxCare;
use App\Models\BoxCollocation;
use App\Models\BoxRetreat;
use App\Models\UserAddress;
use App\Models\BoxAddress;
use App\Models\Comment;
use App\Models\occupation;
use App\Models\position;
use App\Models\Feedback;
use App\Models\BoxGiveThank;
use App\Models\BoxDeliver;
use App\Http\Controllers\Api\V1\AccountController;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Hash;
use Tymon\JWTAuth\Facades\JWTAuth;
use Validator;

class BoxController extends Controller
{
    //盒子列表
   public function postPageRows(Request $request){
        $user = JWTAuth::parseToken()->authenticate();
        $status =  intval($request->input('status'));
        $query = Box::where(['wechat_id'=>$user->id]);
        if ($status) {
            $query->where('status',$status);
        }

        $rows = $query ->paginate(16);
        foreach ($rows as $key => $row) {
           $row->statusName = $row->getStatusName();
        }
        $box_notNumber = Box::where('wechat_id',$user->id)->where('status',1)->count('id');//未使用的盒子数量
        $data = [
           'boxRows'=>$rows,
           'box_notNumber' => $box_notNumber,
        ];
       return response()->json(['code' => 1,'msg' => 'Success','data' => $data]);
   }

   public function postBoxGivePageRows(Request $request){
       $user = JWTAuth::parseToken()->authenticate();
       $status =  intval($request->input('status'));
       $query = Box::where(['to_wechat_id'=>$user->id]);
       if ($status) {
           $query->where('status',$status);
       }

       $rows = $query->paginate(15);
       foreach ($rows as $key => $row) {
          $row->statusName = $row->getStatusName();
       }
       $data = [
          'boxRows'=>$rows,
       ];
      return response()->json(['code' => 1,'msg' => 'Success','data' => $data]);
   }

   //盒子详情
   public function postBoxInfo($id){
        $user = JWTAuth::parseToken()->authenticate();
        $row = Box::find($id);
        if(empty($row)){
           return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
        }
        if ($row->wechat_id != $user->id && $row->to_wechat_id != $user->id) {
            return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
        }

        $row->statusName = $row->getStatusName();
        $collocation = BoxCollocation::where('box_id',$id)->get();
        foreach($collocation as $val){
            $val->pic = url($val->pic);
            $retreat = BoxRetreat::where(['box_id'=>$id,'wechat_id'=>$user->id,'collocation_id'=>$val->id])->first();
            if ($retreat && $retreat->status > 2) {
              $retreat->to_send = unserialize($retreat->to_send); 
            }
            $val->retreat = $retreat;
        }
        $row->collocation = $collocation;
        $row->address = BoxAddress::where('box_id',$row->id)->where('type','box')->first();//邮寄地址
        $row->delive = BoxDeliver::where(['box_id'=>$row->id,'type'=>'box'])->first();//发货信息
        $row->comment = Comment::where('box_id',$id)->first();
        $row->is_Need = false;
        $row->photo = url($row->photo);
        //判断盒子否可用现在就要
        $box = Box::where(['wechat_id'=>$user->id,'status'=>1])->orderBy('delivery_at','ASC')->first();
        if (!empty($box)) {
            if ($row->id == $box->id || ( empty($box) && $row->status == 1) ) {
                $row->is_Need = true;
            }
        }
        if ($row->type==1) {
            $row->GiveThank = BoxGiveThank::where('box_id',$row->id)->first();
        }

        $data = compact('row');
        return response()->json(['code' => 1,'msg' => 'Success','data' => $data]);
   }

    //完善盒子资料
    public function postBoxUpdate(Request $request,$box_id){
        $user = JWTAuth::parseToken()->authenticate();
        $wechat_id = $user->id;
        $box =  Box::find($box_id);
        if(empty($box)){
           return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
        }

        if ($box->type===1) {
            if ($box->wechat_id == $user->id && $request->exists('to_wechat_id')) {
                return response()->json(['code' => -1,'msg' => '本人不可分享给本人','data' => null]);
            }
            $box->to_wechat_id = intval($request->input('to_wechat_id'));//受赠人id
        }elseif($box->type===0){
            if ($box->wechat_id != $user->id) {
                return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
            }
        }else{
            return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
        }
        //-------------------------第一页身形大数据----------------------//
        if ($request->exists('sex')) {
            $box->sex = intval($request->input('sex'));//性别
        }
        if ($request->exists('age')) {
            $box->age = intval($request->input('age'));//年龄
        }
        if ($request->exists('height')) {
            $box->height = intval($request->input('height'));//cm
        }
        if ($request->exists('weight')) {
            $box->weight = intval($request->input('weight'));//kg
        }

        if ($request->exists('jacket_size')) {
            $box->jacket_size = safe2($request->input('jacket_size'));//上衣尺码
        }

        if ($request->exists('pants_size')) {
            $box->pants_size = safe2($request->input('pants_size'));//裤子尺码
        }
        if ($request->exists('shoes_size')) {
            $box->shoes_size = safe2($request->input('shoes_size'));//鞋子尺码
        }

        if ($request->exists('skin_color')) {
            $box->skin_color = safe2($request->input('skin_color'));//肤色
        }

        if ($request->exists('figure')) {
            $box->figure = safe2($request->input('figure'));//身形
        }

        //-------------------------第一页END----------------------//

        //-------------------------第二页----------------------//
        if ($request->exists('color')) {
            $box->color = serialize($request->input('color'));   //颜色喜好
            //格式color['黑色']=['color'=>'#000','type'=>1]  type 1 会穿  2可尝试  3不穿
        }
        //-------------------------第二页END----------------------//
        if ($request->exists('life')) {
            $box->life = safe2($request->input('life'));   //生活场景
        }
        //-------------------------第三页----------------------//

        //-------------------------第四页----------------------//
        if ($request->exists('photo')) {
            $box->photo = str_replace(url('').'/','',safe2($request->input('photo')));   //全身照片  数组
        }
        //-------------------------第四页END----------------------//

        //-------------------------第五页----------------------//
        if ($request->exists('occupation')) {
            $box->occupation = safe2($request->input('occupation'));   //行业
        }
        if ($request->exists('position')) {
            $box->position = safe2($request->input('position'));   //职位
        }
        if ($request->exists('birthday')) {
            $box->birthday = safe2($request->input('birthday'));   //生日
        }
        if ($request->exists('thing')) {
            $box->thing = serialize($request->input('thing'));   //一起完成的事情 多选 数组
        }
        if ($request->exists('other')) {
            $box->other = safe2($request->input('other'));//其他/特别注意
        }
        //-------------------------第五页END----------------------//
        if ($request->exists('delivery_at')) {
            $box->delivery_at = safe2($request->input('delivery_at'));//发货时间
        }

        $box->save();
        if ($request->exists('user_address_id')) {
            $param = [
               'wechat_id'=>$wechat_id,
               'box_id'=>$box_id,
               'user_address_id'=>intval($request->input('user_address_id')),
               'type'=>'box',
           ];
           BoxAddress::boxCopyAddress($param);
        }
        $Profile = UserProfile::where(['wechat_id'=>$wechat_id])->first();
        if (!$Profile) {
            $account = new AccountController;
            $account->AccountCreate($request);
        }
        if ($request->exists('is_synchro') && intval($request->input('is_synchro')) ===1 ) {
            $account = new AccountController;
            unset($request['is_synchro']);//避免更新用户基础资料也会同步盒子
            $account->AccountCreate($request);
        }
        return response()->json(['code' => 1,'msg' => '操作完成','data' => $box]);
    }

    /**
     * 赠送盒子并且冻结盒子
     * @author wuyun
     * @date           2018-08-30T13:44:09+0800
     * @param  Request $request                 [description]
     * @return [type]                           [description]
     */
    public function shareGive(Request $request){
        $user = JWTAuth::parseToken()->authenticate();
        $boxId = intval($request->input('boxId'));
        $validator = Validator::make($request->all(),
            [
                'boxId' => 'required|integer',
            ],[
                'required' => '字段 :attribute 不能为空.',
                'integer'=>'字段 :attribute 必须是数值',
            ]);
        if ($validator->fails()) {
            $messages = $validator->errors();
            return response()->json(['code' => -1,'msg' => $messages->first(),'data' => '',]);
        }
        $box = Box::where(['id'=>$boxId,'wechat_id'=>$user->id,'type'=>1])->first();
        if (empty($box)) {
            return response()->json(['code' => -1,'msg' => '赠送盒子不存在','data' => null]);
        }
        $box->sex= 1;
        $box->age= 0;
        $box->height= 0;
        $box->weight= 0;
        $box->jacket_size= '';
        $box->pants_size= '';
        $box->shoes_size= '';
        $box->skin_color= '';
        $box->figure= '';
        $box->color= '';
        $box->life= '';
        $box->photo= '';
        $box->occupation= '';
        $box->position= '';
        $box->birthday= '';
        $box->thing= '';
        $box->other= '';
        $box->to_wechat_id= -1;//0 未赠送   大于0 赠送成功 赠送人  -1 冻结
        $box->save();
        $wecaht = new WechatController();
        $filename = $wecaht->appCode("pages/share_box/index?box_id={$boxId}&nickname={$user->nickname}",$box->id);
        return response()->json(['code' => 1,'msg' => 'Success','data' => url($filename)]);
    }




   /**
    * 添加评分
    * @author wuyun
    * @date           2018-07-24T17:10:32+0800
    * @param  Request $request                 [description]
    * @return [type]                           [description]
    */
   public function postBoxComment(Request $request){
       $user = JWTAuth::parseToken()->authenticate();
       $boxId = intval($request->input('boxId'));
       $input = $request->all();
       $validator = Validator::make($input,
           [
               'boxId' => 'required|integer',
               'evaluate' => 'required|integer',
               'score' => 'required|integer',
               'clothing_score' => 'required',
               'type' => 'required',
           ],[
               'required' => '字段 :attribute 不能为空.',
               'integer'=>'字段 :attribute 必须是数值',
               'array'=>'字段 :attribute 必须是数组',
           ]);
       if ($validator->fails()) {
           $messages = $validator->errors();
           return response()->json(['code' => -1,'msg' => $messages->first(),'data' => '',]);
       }

       $evaluate = intval($request->input('evaluate'));
       $score = intval($request->input('score'));
       $clothing_score = $request->input('clothing_score');
       $content = safe2($request->input('content'));

       $type = $request->input('type');
       if ($type==1) {
           $row = Box::where(['id'=>$boxId,'wechat_id'=>$user->id])->first();
       }else{
           $row = BoxCare::where(['id'=>$boxId,'wechat_id'=>$user->id])->first();
       }
       if(empty($row)){
           return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
       }

       $comment = new Comment;
       $comment->evaluate= $evaluate;
       $comment->box_id= $boxId;
       $comment->score= $score;
       $comment->content= $content;
       $comment->clothing_score= serialize($clothing_score);
       $comment->wechat_id = $user->id;
       $comment->type = $type;
       $comment->save();
       $row->status=6;
       $row->save();

       return response()->json(['code' => 1,'msg' => 'Success','data' => $comment]);
   }

   /**
    * 换货
    * @param  Request $request [description]
    * @return [type]           [description]
    */
    public function postBoxRetreat(Request $request,$id){
        $user = JWTAuth::parseToken()->authenticate();
        $pic = $request->input('pic');
        $reason = $request->input('reason');
        $demand = $request->input('demand');
        $validator = Validator::make( $request->all(),
            [
               'collocation_id' => 'required|integer',
               'type' => 'required|integer|between:1,2',
               'reason' => 'required|string',
               'pic' => 'array',
               'demand' => 'string',
            ],[
               'required' => '字段 :attribute 不能为空.',
               'integer'=>'字段 :attribute 必须是数值',
               'array'=>'字段 :attribute 必须是数组',
               'between'=>'字段 :attribute 必须在 :min 和 :max 之间'
            ]);
        if ($validator->fails()) {
            $messages = $validator->errors();
            return response()->json(['code' => -1,'msg' => $messages->first(),'data' => '',]);
        }
        $box = Box::where(['id'=>$id,'wechat_id'=>$user->id])->first();
        if (empty($box)) {
            return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
        }

        $collocation_id = intval($request->input('collocation_id'));
        $row = BoxRetreat::where(['box_id'=>$id,'collocation_id'=>$collocation_id])->first();
        if(!empty($row)){
            return response()->json(['code' => -1,'msg' => '已提交申请','data' => null]);
        }
        $new =  new BoxRetreat;
        $new->wechat_id = $user->id;
        $new->box_id= $id;
        $new->collocation_id= $collocation_id;
        $new->pic  = serialize($pic);
        $new->reason = $reason;
        $new->demand = $demand;
        $new->status = 1;//2待发货 3 发货 0申请 1 通过
        $new->type = 1;
        $new->Save();
        return response()->json(['code' => 1,'msg' => '已提交申请','data' =>$new]);
    }


    //换货寄回填写快递信息
    public function postBoxRetreatExpress(Request $request,$box_id){
        $user = JWTAuth::parseToken()->authenticate();
        $validator = Validator::make( $request->all(),
            [
                'collocation_id'=>'required',
                'no' => 'required',
                'name' => 'required',
            ],[
                'required' => '字段 :attribute 不能为空.',
            ]);
        if ($validator->fails()) {
            $messages = $validator->errors();
            return response()->json(['code' => -1,'msg' => $messages->first(),'data' => '',]);
        }
        $collocation_id = intval($request->input('collocation_id'));
        $new =  BoxRetreat::where(['wechat_id' => $user->id,'box_id' => $box_id,'collocation_id' => $collocation_id])->first();
        if (empty($new)) {
            return response()->json(['code' => -1,'msg' => '换货记录不存在','data' => null]);
        }
        $new->name = safe2($request->input('name'));
        $new->no= safe2($request->input('no'));
        $new->status = 2;//2待发货 3 发货 0申请 1 通过
        $new->Save();
        return response()->json(['code' => 1,'msg' => 'Success','data' => $new]);
    }

    /**
     * [postBoxChangeStatus description]现在就要
     * @author wuyun
     * @date           2018-08-30T13:43:47+0800
     * @param  Request $request                 [description]
     * @param  [type]  $box_id                  [description]
     * @return [type]                           [description]
     */
    public function postBoxChangeStatus(Request $request,$box_id){
        $user = JWTAuth::parseToken()->authenticate();
        $row = Box::where(['id'=>$box_id,'wechat_id'=>$user->id])->first();
        if(empty($row)){
            return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
        }
        $address = BoxAddress::where('box_id',$row->id)->where('type','box')->first();
        if (empty($address)) {
            return response()->json(['code' => -1,'msg' => '请填写收件信息后再申请需要','data' => null]);
        }

        $needLog = DB::table('box_need_log')->where('wechat_id',$user->id)->orderBy('id','DESC')->first();
        if ($needLog) {
            $d = time() - strtotime($needLog->created_at);  
            $Days = ceil($d / 3600 / 24); 
            if ($Days < 60) {
               return response()->json(['code' => -1,'msg' => '我们需要两个月精心为您准备新品搭配哦^_^','data' => null]);
            }
        }
        if ($request->input('status')) {
            $row->status= intval($request->input('status'));
        }
        $row->save();
        DB::table("box_need_log")->insert(['wechat_id'=>$user->id,'box_id'=>$row->id,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')]);
        return response()->json(['code' => 1,'msg' => '提示收到您的请求，准备搭配中','data' => null]);
    }

    /**
     * 盒子搭配列表
     * @date           2018-09-13T14:38:40+0800
     * @param  Request $request                 [description]
     * @param  [type]  $box_id                  [description]
     * @return [type]                           [description]
     */
    public function postBoxCollocationPageRows(Request $request,$box_id){
        $user = JWTAuth::parseToken()->authenticate();
        $box = Box::where(['id'=>$box_id,'wechat_id'=>$user->id])->first();
        if (empty($box)) {
            return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
        }
        $rows = BoxCollocation::where('box_id',$box_id)->get();
        foreach($rows as $row){
            $row->pic = url($row->pic);
            $row->retreat = BoxRetreat::where(['box_id'=>$box_id,'wechat_id'=>$user->id,'collocation_id'=>$row->id])->first();
        }
        $data = ['rows'=>$rows];
        return response()->json(['code' => 1,'msg' => 'Success','data' => $data]);
    }

    /**
     * 赠送盒子感谢语
     * @date           2018-09-13T14:38:59+0800
     * @param  Request $request                 [description]
     * @param  [type]  $box_id                  [description]
     * @return [type]                           [description]
     */
    public function postBoxGiveThank(Request $request,$box_id){
        $user = JWTAuth::parseToken()->authenticate();
        $content = safe2($request->input('content'));
        $box = Box::where(['id'=>$box_id,'to_wechat_id'=>$user->id])->first();
        if (empty($box)) {
          return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
        }
        $n = BoxGiveThank::where('wechat_id',$user->id)->where('box_id',$box_id)->count('id');
        if ($n > 0) {
            return response()->json(['code' => -1,'msg' => '感谢已存在','data' => null]);
        }
        $row = new BoxGiveThank;
        $row->box_id = $box_id;
        $row->wechat_id = $user->id;
        $row->content = $content;
        $row->save();
        return response()->json(['code' => 1,'msg' => 'Success','data' => $row]);
    }

    //修改地址
    /**
     * 修改地址
     * @param  Request $request
     * @param  [type]  $box_id   盒子id 或者关怀盒子id
     * @return [type]  $type   box/care
     */
    public function postBoxAddressUpate(Request $request,$box_id){
        $user = JWTAuth::parseToken()->authenticate();
        $validator = Validator::make($request->all(),
        [
            'phone' => ['regex:/^1[3|4|5|6|7|8|9][0-9]\d{4,8}$/'],
            'type' => 'required',
        ],[
            'required' => ':attribute 不能为空.',
            'integer'=>':attribute 必须是数值',
            'regex'=>':attribute 格式不正确',
        ]);
        if ($validator->fails()) {
            $messages = $validator->errors();
            return response()->json(['code' => -1,'msg' => $messages->first(),'data' => '',]);
        }
        $input = $request->all();
        $input['wechat_id'] = $user->id;
        $result = BoxAddress::boxUpdate($box_id,$input);
        if ($result['code']==false) {
            return response()->json(['code' => -1,'msg' => $result['msg'],'data' => null]);
        }
        return response()->json(['code' => 1,'msg' => 'Success','data' => $result['data']]);
    }



  //----------------------关怀盒子----------------------
  /**
   * 关怀盒子列表
   * @author wuyun
   * @date           2018-08-30T13:43:24+0800
   * @param  Request $request                 [description]
   * @return [type]                           [description]
   */
  public function postCarePageRows(Request $request){
      $user = JWTAuth::parseToken()->authenticate();
      $status = intval($request->input('status'));
      $query = BoxCare::where('wechat_id',$user->id);
      if ($status) {
          $query->where('status',$status);
      }
      $rows = $query->paginate(15);
      foreach ($rows as $key => $row) {
          $row->statusName = $row->getStatusName();
          $row->address = BoxAddress::where('box_id',$row->id)->where('type','care')->first();
          //已发货
      }
      $data = ['rows'=>$rows];
      return response()->json(['code' => 1,'msg' => 'Success','data' => $data]);
  }

  /**
   * 关怀盒子详情
   * @author wuyun
   * @date           2018-08-30T13:43:15+0800
   * @param  Request $request                 [description]
   * @param  [type]  $id                      [description]
   * @return [type]                           [description]
   */
  public function postCareInfo(Request $request,$id){
      $user = JWTAuth::parseToken()->authenticate();
      $row = BoxCare::where(['wechat_id'=>$user->id])->find($id);
      if(empty($row)){
         return response()->json(['code' => -1,'msg' => '关怀盒子不存在','data' => null]);
      }
      $row->statusName = $row->getStatusName();
      // $row->collocation = BoxCollocation ::where('box_id',$id)->get();
      $row->address = BoxAddress::where('box_id',$row->id)->where('type','care')->first();
      $row->delive = BoxDeliver::where(['box_id'=>$row->id,'type'=>'care'])->first();
      $data = compact('row');
      return response()->json(['code' => 1,'msg' => 'Success','data' => $data]);
  }

  /**
   * 关怀盒子完善资料
   * @author wuyun
   * @date           2018-08-30T13:43:06+0800
   * @param  Request $request                 [description]
   * @return [type]                           [description]
   */
  public function postCareSubmitData(Request $request)
  {
      $boxId  = intval($request->input('boxId'));//关怀id
      $user = JWTAuth::parseToken()->authenticate();
      if (!$boxId) {
          return response()->json(['code' => -1,'msg' => '缺少ID参数','data' => null]);
      }
      $validator = Validator::make($request->all(),
          [
              'title' => 'required',
              'sex' => 'required',
              'occupation' => 'required',
              'age' => 'required',
              'hobby' => 'required',
              'to_word' => 'required',
          ],[
              'title.required'=>'称谓不能为空',
              'sex.required'=>'性别不能为空',
              'occupation.required'=>'职业不能为空',
              'age.required'=>'年龄不能为空',
              'hobby.required'=>'爱好不能为空',
              'to_word.required'=>'最想对她/他说的话不能为空',
          ]);
      if ($validator->fails()) {
          $messages = $validator->errors();
          return response()->json(['code' => -1,'msg' => $messages->first(),'data' => '',]);
      }
      $row = BoxCare::where(['id'=>$boxId,'wechat_id'=>$user->id])->first();
      if(empty($row)){
          return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
      }
      if ($row->is_frozen < 1) {
          return response()->json(['code' => -1,'msg' => '该关怀盒子还未激活','data' => null]);
      }
      if ($row->status >3) {
          return response()->json(['code' => -1,'msg' => '盒子已发货无法更改信息','data' => null]);
      }
      $input = [
        'title'=> $request->input('title'),
        'sex'  => $request->input('sex'),
        'occupation'  => $request->input('occupation'),
        'age'  => intval($request->input('age')),
        'hobby'  => $request->input('hobby'),
        'to_word'  => $request->input('to_word'),
        'status'  => 3,
      ];
      if ($request->exists('user_address_id')) {
            $param = [
               'wechat_id'=>$user->id,
               'box_id'=>$boxId,
               'user_address_id'=>intval($request->input('user_address_id')),
               'type'=>'care',
           ];
           BoxAddress::boxCopyAddress($param);
      }
      $box = BoxCare::where(['id'=>$boxId,'wechat_id'=>$user->id])->update($input);
      return response()->json(['code' => 1,'msg' => 'Success','data' => $box]);
  }

  /**
   * [postUntieBoxCare description]激活关怀盒子
   * @author wuyun
   * @date           2018-08-30T13:42:43+0800
   * @param  Request $request                 [description]
   * @return [type]                           [description]
   */
  public function postUntieBoxCare(Request $request,$boxId){
      $user = JWTAuth::parseToken()->authenticate();
      $row = BoxCare::where(['id'=>$boxId,'wechat_id'=>$user->id])->first();
      if(empty($row)){
          return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
      }
      $row->is_frozen=1;
      $row->save();
      return response()->json(['code' => 1,'msg' => 'Success','data' => null]);
  }
  //----------------------end关怀盒子----------------------

    //盒子签收
    public function postBoxSign(Request $request,$boxId){
        $validator = Validator::make($request->all(),
            [
                'type' => 'required',
            ],[
                'required'=>'字段 :attribute 不能为空.',
            ]);
        if ($validator->fails()) {
            $messages = $validator->errors();
            return response()->json(['code' => -1,'msg' => $messages->first(),'data' => '',]);
        }
        $type = $request->input('type');
        $user = JWTAuth::parseToken()->authenticate();
        if ($type=='box') {
            $row = Box::where(['id'=>$boxId,'wechat_id'=>$user->id])->first();
        }elseif($type=='care'){
            $row = BoxCare::where(['id'=>$boxId,'wechat_id'=>$user->id])->first();
        }else{
            return response()->json(['code' => -1,'msg' => '类型不存在','data' => null]);
        }
        if (empty($row)) {
            return response()->json(['code' => -1,'msg' => '盒子不存在','data' => null]);
        }
        if ($row->status>=5) {
            return response()->json(['code' => -1,'msg' => '盒子已签收','data' => null]);
        }

        $row->status = ($type=='box') ? 5 : 6;
        $row->Save();
        BoxDeliver::where(['box_id'=>$row->id,'type'=>$type])->update(['sign_at'=>date('Y-m-d H:i:s')]);
        return response()->json(['code' => 1,'msg' => 'Success','data' => null]);
    }
}
